module view {
    export class AdventureInfoView extends eui.Component{
        supplyLabel1: eui.Label
        supplyLabel2: eui.Label
        supplyBtn1: eui.Button
        supplyBtn2: eui.Button
        useBtn1: eui.Button
        useBtn2: eui.Button
        bagBtn: eui.Button
        closeBtn: eui.Button
        hpLabel:eui.Label
        energyLabel: eui.Label
        luckyLabel: eui.Label
        parentView: eui.Component
        supplyIndex: number
        littleBag: eui.Component
		public constructor(parentView) {
            super()
            this.skinName = 'resource/control/AdventureInfoSkin.exml'
            this.parentView = parentView
            this.name = 'AdventureInfoView'
            this.bagBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.bag,this)
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.supplyBtn1['value'] = 0
            this.supplyBtn1.addEventListener(egret.TouchEvent.TOUCH_TAP,this.openLittleBag,this)
            this.supplyBtn2['value'] = 1
            this.supplyBtn2.addEventListener(egret.TouchEvent.TOUCH_TAP,this.openLittleBag,this)
                
            this.useBtn1['value'] = 0
            this.useBtn1.addEventListener(egret.TouchEvent.TOUCH_TAP,this.useSupply,this)
            this.useBtn2['value'] = 1
            this.useBtn2.addEventListener(egret.TouchEvent.TOUCH_TAP,this.useSupply,this)
            this.showPlayerInfo() 
            this.showSupply() 
		}
		
        private close(evt: egret.TouchEvent): void {
            this.stage.removeChild(this.parentView)
        }
        
        private bag(evt: egret.TouchEvent): void {
            var child = new view.BagView()
            this.stage.addChild(child)
        }
        
        public showPlayerInfo() { 
            this.hpLabel.text = gplayer.fightAttri.hp + ''
            this.energyLabel.text = gplayer.basicInfo.energy + ''
            this.luckyLabel.text = gplayer.fightAttri.lucky + ''
        }
        
        public showSupply() {
            if(gplayer.supplyBar[0]){
                var id = gplayer.supplyBar[0].id
                var itemName = totalConfig[id].name
                this.supplyLabel1.text = itemName + 'X' + gplayer.supplyBar[0].stack
            }else{
                this.supplyLabel1.text = ''
            }
            
            if(gplayer.supplyBar[1]) {
                var id = gplayer.supplyBar[0].id
                var itemName = totalConfig[id].name
                this.supplyLabel2.text = itemName + 'X' + gplayer.supplyBar[1].stack
            } else {
                this.supplyLabel2.text = ''
            }            
        }
        
        private useSupply(evt: egret.TouchEvent){
            var target = evt.target
            var supplyIndex = target.value
            if(gplayer.supplyBar[supplyIndex] && gplayer.fightAttri.hp !== gplayer.fightAttri.maxHp) {
                var item = gplayer.supplyBar[supplyIndex]
                var itemConfig = totalConfig[item.id]
                gplayer.fightAttri.hp += itemConfig.value
                if(gplayer.fightAttri.hp > gplayer.fightAttri.maxHp){
                    gplayer.fightAttri.hp = gplayer.fightAttri.maxHp
                }
                item.stack -= 1
                if(item.stack === 0){
                    gplayer.supplyBar[supplyIndex] = null
                }
                this.showPlayerInfo() 
                this.showSupply() 
            }           
        }
        
        public setPlayerInfo():void {
            this.hpLabel.text = gplayer.fightAttri.hp + ''
            this.luckyLabel.text = gplayer.fightAttri.lucky + ''
            this.energyLabel.text = gplayer.basicInfo.energy + ''
        }
        
         //打开小背包
        private openLittleBag(evt: egret.TouchEvent):void{
            var target = evt.target
            this.supplyIndex = target.value
            var littleBag = new eui.Component()
            littleBag.skinName = 'resource/control/Bag1Skin.exml'           
            this.parentView.addChild(littleBag)
            this.littleBag = littleBag
            this.littleBag['bgButton'].addEventListener(egret.TouchEvent.TOUCH_TAP,function() {
                this.parentView.removeChild(this.littleBag)
                },this)
        
            for(var i in gplayer.bag.items) {
                var item = gplayer.bag.items[i]
                if(!item) {
                    continue
                }
                var itemId = item.id
                var itemConfig = totalConfig[itemId]
                if(itemConfig.propType !== 2) {
                    continue
                }
                var btn: eui.Button = new eui.Button()
                btn.skinName = "resource/control/Button3Skin.exml"
                btn.width = 160
                littleBag['itemsGroup'].addChild(btn)
                var itemId = item.id
                var stack = item.stack
                btn.label = templateName[itemId] + 'X' + stack
                btn['value'] = itemId
                btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.supply,this)
            }
        }
        //添加物品到补给栏
        private supply(evt:egret.TouchEvent){
            var target = evt.target
            var itemId = target.value
            if(gplayer.supplyBar[this.supplyIndex]){
                var itemComponent = gplayer.supplyBar[this.supplyIndex]
                gplayer.bag.addItem(itemComponent)
                gplayer.supplyBar[this.supplyIndex] = null
            }
            
            var itemComponent = gplayer.bag.getItemComponent(itemId)
            gplayer.supplyBar[this.supplyIndex] = itemComponent 
            gplayer.bag.removeItemById(itemId)
            this.showSupply() 
            this.parentView.removeChild(this.littleBag)
        }
	}
	
	
}
